<?php
class IndexAction extends HomeBaseAction
{
    public function index()
    {
        $this->title = '首页';
        $this->display('Home:index');
    }

    public function cat()
    {
        if(!$_GET['id']) redirect(U('index'));
        $db = D('Class');
        $io = $db->find($_GET['id']);

        //dump($io);
        $this->title = $io['name'];
        $tpl = unserialize($io['tpl']);
        $tpl = $tpl['list'];
        if($tpl == 'article/list') {
            $this->getList($io['cid']);
        }
        if($io['associate']) {
            $_GET['id'] = $io['associate'];
            $this->show();
        }
        unset($io);
        //$this->display('Home:'.$tpl);
    }

    public function show()
    {
        // 验校ID
        if(!is_numeric($_GET['id'])) $this->error('非法访问');
        $db = D('Article');
        $io = $db->field('a.*, c.tpl, c.name')->where("a.status='0' AND a.id='{$_GET['id']}'")->join("a LEFT JOIN `{$this->bt}class` c ON c.cid=a.cid")->find();
        $io['tpl'] = unserialize($io['tpl']);
        $tpl = $io['tpl']['show'];
        // 二次验证
        if($_GET['id'] != $io['id']) $this->error('非法访问');
        $this->title = $io['title'];
        $this->assign('io', $io);
        $this->display('Home:'.$tpl);
    }

    private function getList($cid)
    {
        Import('Org.Util.Page');
        $db = D('Article');
        $page = new Page($db->where("`status`='0' AND `cid`='$cid'")->count(), 5);
        $list = $db->field('id,img,title,introduction')->where("`cid`='$cid' AND `status`='0'")->limit($page->firstRow . ',' . $page->listRows)->order()->select();
        //echo $db->getLastSql();
        //dump($list);
        $this->assign('list', $list);
        $this->assign('page',$page->show());
    }
}
?>